package com.fyt.ZhongYaoApp.Controller;

import com.fyt.ZhongYaoApp.pojo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> fileUpload(@RequestParam("file") MultipartFile file) throws IOException {
        // 把文件的内容存储到本地磁盘上
        String fileName = file.getOriginalFilename();
        // 保证文件的名字是唯一的，从而防止文件覆盖
        String lastFileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
        file.transferTo(new File("F:\\桌面\\files\\" + lastFileName));
        return Result.success("F:\\桌面\\files\\" + fileName);
    }
}
